<?php

namespace App\Admin\Controllers;

use App\Models\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Row;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户');
            $content->description('列表');

            $content->body($this->grid());
        });
    }
    public function detail($user_id){
        $user = User::find($user_id);
        $oss_prefix = env('OSS_PREFIX');
        return Admin::content(function (Content $content)use($user, $oss_prefix) {

            $content->header('用户');
            $content->description('详情');
            $content->breadcrumb(
                ['text' => '首页', 'url' => '/admin'],
                ['text' => '用户列表', 'url' => '/users'],
                ['text' => '编辑用户']
            );

            $content->row(function(Row $row)use($user,$oss_prefix){
                $row->column(1,'');
                $row->column(2, function(Column $column)use($user,$oss_prefix){
                    $column->append("<img src='".$user['face_url']."' alt='头像'/>")->append('<br/>')->append('<br/>');
                    $column->append('<b>ID: </b>'.$user['id'])->append('<br/>')->append('<br/>');
                    $column->append('<b>用户昵称: </b>'.$user['nick_name'])->append('<br/>')->append('<br/>');
                    $column->append('<b>书签数量: </b>'.count($user->bookmark))->append('<br/>')->append('<br/>');
                    $column->append('<b>长评数量: </b>'.count($user->sight))->append('<br/>')->append('<br/>');
                    $column->append('<b>搜索次数: </b>'.count($user->search))->append('<br/>')->append('<br/>');
                    $column->append('<b>阅读章数: </b>'.count($user->readHistory))->append('<br/>')->append('<br/>');
                    $column->append('<b>注册时间: </b>'.$user->created_at);

                });
                $row->column(2,function(Column $column)use($user){

                });
            });
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('用户信息');
            $content->description('所有用户');
            $content->breadcrumb(
                ['text' => '首页', 'url' => '/admin'],
                ['text' => '用户管理', 'url' => '/admin/users'],
                ['text' => '编辑用户']
            );
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {

//            $grid->id('ID')->sortable();
            $grid->id('ID')->display(function($id){
                return "<a href='users/detail/{$id}'>$id</a>";
            });
            $grid->nick_name('昵称');
            $grid->email('邮箱');
            $grid->reading_num('看书数量');
            $grid->bookmark('书签数量')->display(function($bookmark){
                $count = count($bookmark);
                return "<span class='label label-warning'>{$count}</span>";
            });
            $grid->search('搜索次数')->display(function($search){
                $count = count($search);
                return "<span class='label label-warning'>{$count}</span>";
            });
            $grid->sight('长评条数')->display(function($sight){
                $count = count($sight);
                return "<span class='label label-warning'>{$count}</span>";
            });
            $grid->readHistory('阅读章数')->display(function($read){
                $count = count($read);
                return "<span class='label label-warning'>{$count}</span>";
            });
            $grid->created_at('注册时间');
            $grid->updated_at('更新时间');
            $grid->filter(function ($filter) {
                // 设置created_at字段的范围查询
                $filter->between('created_at', 'Created Time')->datetime();
            });

            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
            });
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('nick_name','昵称');
            $form->display('email','邮箱');
            $form->display('created_at', '注册时间');
            $form->display('updated_at', '更新时间');
        });
    }
}
